package com.yeebo.admin.domain;

import com.yeebo.admin.dao.SysResourceDao;
import com.yeebo.enums.ResultCode;
import com.yeebo.enums.admin.ResourceLevelEnums;
import com.yeebo.exception.ServiceException;
import com.yeebo.model.admin.SysResource;
import com.yeebo.vo.admin.SysResourcesTreeVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Component("sysResourceDomain")
public class SysResourceDomain {

    @Resource
    private SysResourceDao sysResourceDao;
    @Resource
    private SysRoleDomain sysRoleDomain;

    public List<SysResourcesTreeVO> loadMenuManageList(String filterMenuName) {
        List<SysResource> allValidResources = sysResourceDao.loadAllValidResources();
        if (StringUtils.isNotBlank(filterMenuName)) {
            allValidResources = allValidResources.stream().filter(s -> s.getMenuName().contains(filterMenuName)).collect(Collectors.toList());
        }
        List<SysResource> firstLevelResources = allValidResources.stream().filter(s -> s.getResourceLevel().equals(ResourceLevelEnums.ONE.getLevel())).collect(Collectors.toList());
        List<SysResource> secondLevelResources = allValidResources.stream().filter(s -> s.getResourceLevel().equals(ResourceLevelEnums.TWO.getLevel())).collect(Collectors.toList());
        List<SysResourcesTreeVO> resultList = sysRoleDomain.generateNeedResourceList(firstLevelResources, secondLevelResources, new LinkedList<>());
        return resultList;
    }

    // 根据用户ID获取用户的菜单列表
    public List<SysResourcesTreeVO> getMenuListByUserId(Long userId) {
        List<SysResource> allValidResources = sysResourceDao.loadResourcesByUserId(userId);
        List<SysResource> firstLevelResources = allValidResources.stream().filter(s -> s.getResourceLevel().equals(ResourceLevelEnums.ONE.getLevel())).collect(Collectors.toList());
        List<SysResource> secondLevelResources = allValidResources.stream().filter(s -> s.getResourceLevel().equals(ResourceLevelEnums.TWO.getLevel())).collect(Collectors.toList());
        List<SysResourcesTreeVO> resultList = sysRoleDomain.generateNeedResourceList(firstLevelResources, secondLevelResources, new LinkedList<>());
        return resultList;
    }

    public List<SysResourcesTreeVO> loadTopLevelMenuList() {
        List<SysResource> allValidResources = sysResourceDao.loadAllValidResources();
        List<SysResource> firstLevelResources = allValidResources.stream().filter(s -> s.getResourceLevel().equals(ResourceLevelEnums.ONE.getLevel())).collect(Collectors.toList());
        List<SysResourcesTreeVO> resultList = sysRoleDomain.generateNeedResourceList(firstLevelResources, new LinkedList<>(), new LinkedList<>());
        return resultList;
    }

    public List<SysResourcesTreeVO> loadResourceManageList(Long parentMenuId, String filterResourceName) {
        List<SysResource> allValidResources = sysResourceDao.loadAllValidResources();
        allValidResources = allValidResources.stream().filter(s -> s.getParentMenuId().equals(parentMenuId)).collect(Collectors.toList());
        if (StringUtils.isNotBlank(filterResourceName)) {
            allValidResources = allValidResources.stream().filter(s -> s.getMenuName().contains(filterResourceName)).collect(Collectors.toList());
        }

        List<SysResourcesTreeVO> resultList = sysRoleDomain.generateNeedResourceList(allValidResources, null, null);
        return resultList;
    }

    public void addSysResource(SysResource addEntity) {
        SysResource checkExistResource = sysResourceDao.getResourceByCode(addEntity.getResourceCode());
        if (checkExistResource != null) {
            throw new ServiceException(ResultCode.RESOURCE_CODE_REPEAT);
        }
        sysResourceDao.add(addEntity);
    }

    public void updateSysResource(SysResource updateEntity) {
        sysResourceDao.update(updateEntity);
    }

    public SysResource getSysResourceById(Long sysResourceId) {
        return sysResourceDao.getResourceById(sysResourceId);
    }

    public void deleteSysResource(Long sysResourceId) {
        // 判断是否有其他资源正在依赖此资源
        List<SysResource> allValidResources = sysResourceDao.loadAllValidResources();
        if (allValidResources.stream().filter(s -> s.getParentMenuId().equals(sysResourceId)).count() > 0) {
            throw new ServiceException(ResultCode.RESOURCE_HAVE_CHILDREN);
        }
        // 开始更新数据
        SysResource existResource = getSysResourceById(sysResourceId);
        existResource.setValid(false);
        updateSysResource(existResource);
    }

}
